package com.mall.goods.controller;


import com.mall.cart.model.Cart;
import com.mall.goods.model.Sku;
import com.mall.goods.service.ISkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mall.common.util.RespResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;


/**
 * <p>
 * 商品SKU表 控制器
 * </p>
 *
 * @author Darren
 * @since 2022-08-18
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(tags = "REST - 商品SKU表")
@RestController
@RequestMapping("/sku")
public class SkuController {
    private final ISkuService service;

    @PostMapping("/addSku")
    @ApiOperation(value = "新增")
    public RespResult addSku(@RequestBody Sku sku) {
        boolean flag = service.addSku(sku);
        return RespResult.ok(flag ? "创建成功" : "创建失败");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除")
    public RespResult deleteById(@PathVariable("id") String id) {
        boolean flag = service.deleteById(id);
        return RespResult.ok(flag ? "删除成功" : "删除失败");
    }

    @PutMapping("/updateSku")
    @ApiOperation(value = "更新")
    public RespResult updateSku(@RequestBody Sku sku) {
        boolean flag = service.updateSku(sku);
        return RespResult.ok(flag ? "更新成功" : "更新失败");
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public RespResult<Sku> getById(@PathVariable("id") String id) {
        return RespResult.ok(service.getById(id));
    }

    @PostMapping("/selectList")
    @ApiOperation(value = "根据条件查询全部数据")
    public RespResult selectList(Sku sku) {
        return RespResult.ok(service.selectList(sku));
    }

    @PostMapping("/selectPage")
    @ApiOperation(value = "分页查询")
    public RespResult selectPage(Page<Sku> page, Sku sku) {
        return RespResult.ok(service.selectPage(page, sku));
    }

    @GetMapping("/typeSkuItems")
    @ApiOperation(value = "根据推广id查询Sku列表")
    public RespResult<List<Sku>> typeSkuItems(@RequestParam("type") Integer type) {
        return RespResult.ok(service.typeSkuItems(type));
    }

    @DeleteMapping("/typeSkuItems")
    @ApiOperation(value = "根据推广id删除Sku列表缓存")
    public RespResult delTypeSkuItems(@RequestParam("type") Integer type) {
        service.delTypeSkuItems(type);
        return RespResult.ok();
    }

    @PutMapping("/typeSkuItems")
    @ApiOperation(value = "根据推广id修改Sku列表缓存")
    public RespResult updateTypeSkuItems(@RequestParam("type") Integer type) {
        service.updateTypeSkuItems(type);
        return RespResult.ok();
    }

    @PostMapping(value = "/reduceSkuNum")
    @ApiOperation(value = "减少商品库存")
    public RespResult reduceSkuNum(@RequestBody List<Cart> carts) {
        service.reduceSkuNum(carts);
        return RespResult.ok();
    }
}